﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ClientLib.Common;

namespace ClientLib.MonitorState2
{
    class Collection
    {
        #region  ////属性集合
        private string collection_name;
        private List<DeviceDynamic> device_list;
        #endregion

        #region

        public string Collection_Name
        {
            get { return collection_name; }
            set { collection_name = value; }

        }
        #endregion


        #region ////构造函数

        public Collection(string name)
        {
            collection_name = name;
            device_list = new List<DeviceDynamic>();
        }


        public Collection()
        {
            collection_name = "dynamic";
            device_list = new List<DeviceDynamic>();
        }
         
        #endregion

        #region   ///监控设备更改

        /*********************************************
       添加一个监控设备代理
        *********************************************/
        public bool AddDevice(DeviceDynamic device)
        {
            if (device_list.Contains(device))
            {
                return false;
            }
            else
            {
                device_list.Add(device);
                return true;
            }
        }

        
        /*********************************************
        移除一个监控代理
        *********************************************/
        public bool DelDevice(DeviceDynamic device)
        {
            if (device_list.Contains(device))
            {
                device_list.Remove(device);
                return true; ;
            }
            else
            {
                return false;
            }
        }

        /*********************************************
        获取一个监控代理
        *********************************************/
        public DeviceDynamic GetDevice(string dev_id,string mon_id)
        {
            IEnumerable<DeviceDynamic> dev_list = from dev in device_list
                                                  where dev.Device_ID == dev_id
                                                  where dev.Monitor_ID == mon_id
                                                  select dev;
            if (dev_list.Count<DeviceDynamic>() > 0)
            {
                return dev_list.ElementAt<DeviceDynamic>(0);
            }
            return null;
        }

        /*********************************************
        获取监控设备名称集合
        *********************************************/
        public DeviceSign[] GetDeviceIDList()
        {
            IEnumerable<DeviceSign> dev_list = device_list.Select(c => new DeviceSign { dev_id=c.Device_ID,mon_id=c.Monitor_ID });
            return dev_list.ToArray();
        }

        /*********************************************
        获取扫描时间
        *********************************************/
        public int GetScanTime()
        {
            try
            {
                int time = device_list.Select(c => c.Scan_Time)
                                       .Min<int>();
                return time;
            }
            catch (Exception e)
            {
                return 0;
            }
        }

        /*********************************************
       获取设备数量
       *********************************************/
        public int GetDeviceCount()
        {
            return device_list.Count<DeviceDynamic>();
        }

        /*********************************************
       获取监控设备序列
       *********************************************/
        public IEnumerable<DeviceDynamic> GetDeviceList()
        {
            return device_list.Select(c=>c);
        }

        /*********************************************
       设备是否处于监控
       *********************************************/
        public bool IsDeviceMonitor(string dev_id, string mon_id)
        {
            var dev = device_list.Where(c => (c.Device_ID == dev_id && c.Monitor_ID == mon_id))
                                .Select(c=>c.Device_ID);
            if (dev.Count<string>() > 0)
                return true;
            return false;
                             
        }
        #endregion
    }
}
